package com.eastcom.providor.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.eastcom.bm.atom.Atom_DatasetDefine;
import com.eastcom.bm.atom.Atom_DatasetNe;
import com.eastcom.bm.atom.Atom_PublicNe;
import com.eastcom.providor.dao.BaseDao;
import com.eastcom.providor.dao.IDataSetNeDao;

public class DataSetNeDao extends BaseDao implements IDataSetNeDao {

	public List<Atom_DatasetNe> getAllDatasetNe() throws Exception {
		List<Atom_DatasetNe> results=new ArrayList<Atom_DatasetNe>();
		try{
			results = this.getObjects(Atom_DatasetNe.class);
			return results;
		}
		catch(Exception e){
			throw e;
		}
	}

	//public void saveDatasetNes(List<Atom_DatasetNe> datasetNes) {
		//this.saveObjects(datasetNes);
	//}

	public void delDatasetNeByDatasetID(String datasetID) throws Exception {
		this.deleteObjects("delete Atom_DatasetNe where datasetId = ?", new String[]{datasetID});
		
	}

	public void saveDatasetNes(String dataSetId, List<String> publicNeIds) throws Exception {
		if(publicNeIds==null||publicNeIds.isEmpty())
			return;
		
		try{
			Atom_DatasetDefine dataSet=new Atom_DatasetDefine();
            dataSet.setId(dataSetId);
            
            List<Atom_DatasetNe> setNes=new ArrayList<Atom_DatasetNe>();
            
            for(String neId:publicNeIds){
            	Atom_PublicNe publicNe=new Atom_PublicNe();
                publicNe.setId(neId);
                
                Atom_DatasetNe setNe=new Atom_DatasetNe();
                setNe.setDatasetDefine(dataSet);
                setNe.setPublicNe(publicNe);
                
                setNes.add(setNe);
            }
            
            this.saveObjects(setNes);
		}
		catch(Exception e){
			throw e;
		}
	}

	public void editDatasetNes(String dataSetId, List<String> publicNeIds) throws Exception {
		try{
			this.deleteObjects("delete from Atom_DatasetNe n where n.datasetDefine.id='"+dataSetId+"'", null);
			
			Atom_DatasetDefine dataSet=new Atom_DatasetDefine();
            dataSet.setId(dataSetId);
            
            List<Atom_DatasetNe> setNes=new ArrayList<Atom_DatasetNe>();
            
            for(String neId:publicNeIds){
            	Atom_PublicNe publicNe=new Atom_PublicNe();
                publicNe.setId(neId);
                
                Atom_DatasetNe setNe=new Atom_DatasetNe();
                setNe.setDatasetDefine(dataSet);
                setNe.setPublicNe(publicNe);
                
                setNes.add(setNe);
            }
            
            this.saveObjects(setNes);
		}
		catch(Exception e){
			throw e;
		}
	}
	
}
